package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.content.mapper.TeachplanMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 86150
 * @description 针对表【teachplan(课程计划)】的数据库操作Service实现
 * @createDate 2023-07-11 20:26:12
 */
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan>
        implements TeachplanService {

    @Resource
    private TeachplanMapper teachplanMapper;

    @Resource
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    /**
     * 获取最新的排序号
     *
     * @param courseId 课程id
     * @param parentId 父课程计划id
     * @return 最新排序号
     */
    private int getTeachplanCount(Long courseId, Long parentId) {
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count + 1;
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        // 通过课程计划id判断是新增或修改
        Long teachplanId = saveTeachplanDto.getId();
        if (teachplanId == null) {
            // 新增
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            // 确定排序字段，找到它的同级节点个数，排序字段就是个数加1
            // sql: select count(1) from teachplan where course course_id=117 and parentid=268
            Long parentid = saveTeachplanDto.getParentid();
            Long courseId = saveTeachplanDto.getCourseId();
            int count = getTeachplanCount(courseId, parentid);
            teachplan.setOrderby(count);
            teachplanMapper.insert(teachplan);
        } else {
            // 修改
            Teachplan teachplan = teachplanMapper.selectById(teachplanId);
            // 将参数复制到teachplan
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            teachplan.setIsPreview(saveTeachplanDto.getIsPreview());
            teachplanMapper.updateById(teachplan);
        }
    }

    private Teachplan getTeachplan(Long courseId, Long parentid, Integer orderby) {
        // 拼装查询条件
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId, courseId);
        queryWrapper.eq(Teachplan::getParentid, parentid);
        queryWrapper.eq(Teachplan::getOrderby, orderby);
        // 查询数据
        return teachplanMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public void deleteTeachplan(Long id) {
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid, id);
        // 判断是否有子级信息
        Integer count = teachplanMapper.selectCount(queryWrapper);
        if (count > 0) {
            XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
        }

        // 判断是否为第二级别的小章节
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer orderby = teachplan.getOrderby();
        Long courseId = teachplan.getCourseId();
        Long parentid = teachplan.getParentid();
        if (parentid != 0) {
            // 将teachplan_media表关联的信息删除
            LambdaQueryWrapper<TeachplanMedia> deleteWrapper = new LambdaQueryWrapper<>();
            deleteWrapper.eq(TeachplanMedia::getTeachplanId, id);
            teachplanMediaMapper.delete(deleteWrapper);
        }

        // 判断是否为最后一个章节
        if (orderby != getTeachplanCount(courseId, parentid) - 1) {
            // 拼装查询条件
            queryWrapper.clear();
            queryWrapper.eq(Teachplan::getCourseId,courseId);
            queryWrapper.eq(Teachplan::getParentid,parentid);
            queryWrapper.ne(Teachplan::getOrderby,orderby);
            queryWrapper.orderByAsc(Teachplan::getOrderby);

            // 查询当前二级章节所有数据
            List<Teachplan> teachplanList = teachplanMapper.selectList(queryWrapper);

            // 更新当前章节所有排序字段
            int i = 1;
            for (Teachplan tp : teachplanList) {
                tp.setOrderby(i);
                teachplanMapper.updateById(tp);
                i++;
            }
        }
        // 删除课程计划
        teachplanMapper.deleteById(id);
    }

    @Override
    public void moveTeachplan(String moveType, Long id) {
        // 查询当前操作的课程计划数据
        Teachplan teachplan = teachplanMapper.selectById(id);
        Long courseId = teachplan.getCourseId();
        Long parentid = teachplan.getParentid();
        Integer orderby = teachplan.getOrderby();

        Teachplan tempTeachplan;
        if (("movedown").equals(moveType)) {
            if (orderby == getTeachplanCount(courseId, parentid) - 1) {
                // 已经在最后一个无需移动位置
                return;
            }
            // 获取下一个课程计划数据
            tempTeachplan = getTeachplan(courseId, parentid, orderby + 1);
        } else {
            if (orderby == 1) {
                // 已经在第一个无需移动位置
                return;
            }
            // 获取上一个课程计划数据
            tempTeachplan = getTeachplan(courseId, parentid, orderby - 1);
        }

        // 查询要交换的课程计划数据数据
        teachplan.setOrderby(tempTeachplan.getOrderby());
        tempTeachplan.setOrderby(orderby);

        // 修改数据
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(tempTeachplan);

    }


}










